package com.ybxx.core.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import java.util.Vector;

/**
 * <P>Title:时间处理类 </P>
 * <P>Description:此类主要用来取得本地系统的系统时间并用下面5种格式显示</P>
 * <P>1. YYMMDDHH 8位</P>
 * <P> 2. YYMMDDHHmm10位 </P>
 * <P>3. YYMMDDHHmmss 12位</P>
 * <P> 4. YYYYMMDDHHmmss 14位</P>
 * <P> 5. YYMMDDHHmmssxxx 15位 (最后的xxx是毫秒)</P>
 * <P>Copyright: Copyright (c) 2012-2026</P>
 * <P>Company: </P>
 * @author:Oscar.Doodle rat
 * @version 1.0
 */
public class CTime
{
	public static final int YYMMDDhhmmssxxx = 15;

	public static final int YYYYMMDDhhmmss = 14;

	public static final int YYMMDDhhmmss = 12;

	public static final int YYMMDDhhmm = 10;

	public static final int YYMMDDhh = 8;

	public static final int YYYYMMDD = 1;

	
	/**
	 * @Description 得到当前是那一天
	 * @return 返回字符串      格式：yyyy-MM-dd
	 */
	public static String getDate()
	{
		java.text.SimpleDateFormat d = new java.text.SimpleDateFormat();
		d.applyPattern("yyyy-MM-dd");
		java.util.Date nowdate = new java.util.Date();
		String str_date = d.format(nowdate);
		return str_date;
	}
	
	/**
	 * @Description 得到当前时间精确到秒
	 * @return 返回字符串      格式：yyyyMMddhhmmss
	 */
	public static String getReadomTime()
	{
		java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(
				"yyyyMMddhhmmss");
		String CDate = df.format(new java.util.Date());
		return CDate;
	}

	/**
	 * @Description 返回的毫秒数从1970年1月1日,这一天00:00:00
	 * @return 返回long 
	 */
	public static long getMilliTime(String strDate)
	{
		long delta = 0L;
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try
		{
			Date d1 = df.parse(strDate);
			Date d2 = df.parse("1970-01-01 00:00:00");
			long m1 = d1.getTime();
			long m2 = d2.getTime();
			delta = m1 - m2;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return delta;
	}

	/**
	 * @Description 取得本地系统的时间，时间格式由参数决定
	 * 
	 * @param format
	 *            时间格式由常量决定
	 * @return String 具有format格式的字符串
	 */
	public static String getTime(int format)
	{
		StringBuffer cTime = new StringBuffer(15);
		Calendar time = Calendar.getInstance();
		int miltime = time.get(Calendar.MILLISECOND);
		int second = time.get(Calendar.SECOND);
		int minute = time.get(Calendar.MINUTE);
		int hour = time.get(Calendar.HOUR_OF_DAY);
		int day = time.get(Calendar.DAY_OF_MONTH);
		int month = time.get(Calendar.MONTH) + 1;
		int year = time.get(Calendar.YEAR);
		time = null;
		if (format == YYYYMMDD)
		{
			return cTime.append(year).append(getFormatTime(month, 2)).append(
					getFormatTime(day, 2)).toString().trim();
		}
		if (format != 14)
		{
			if (year >= 2000)
				year = year - 2000;
			else
				year = year - 1900;
		}
		if (format >= 2)
		{
			if (format == 14)
				cTime.append(year);
			else
				cTime.append(getFormatTime(year, 2));
		}
		if (format >= 4)
			cTime.append(getFormatTime(month, 2));
		if (format >= 6)
			cTime.append(getFormatTime(day, 2));
		if (format >= 8)
			cTime.append(getFormatTime(hour, 2));
		if (format >= 10)
			cTime.append(getFormatTime(minute, 2));
		if (format >= 12)
			cTime.append(getFormatTime(second, 2));
		if (format >= 15)
			cTime.append(getFormatTime(miltime, 3));
		return cTime.toString().trim();
	}

	/**
	 * @Description 产生任意位的字符串
	 * 
	 * @param iyear
	 *            int 要转换格式的时间
	 * @param format
	 *            int 转换的格式长度
	 * @return String 转换的时间
	 */

	public synchronized static String getYearAdd(int format, int iyear)
	{
		StringBuffer cTime = new StringBuffer(10);
		Calendar time = Calendar.getInstance();
		time.add(Calendar.YEAR, iyear);
		int miltime = time.get(Calendar.MILLISECOND);
		int second = time.get(Calendar.SECOND);
		int minute = time.get(Calendar.MINUTE);
		int hour = time.get(Calendar.HOUR_OF_DAY);
		int day = time.get(Calendar.DAY_OF_MONTH);
		int month = time.get(Calendar.MONTH) + 1;
		int year = time.get(Calendar.YEAR);
		if (format != 14)
		{
			if (year >= 2000)
				year = year - 2000;
			else
				year = year - 1900;
		}
		if (format >= 2)
		{
			if (format == 14)
				cTime.append(year);
			else
				cTime.append(getFormatTime(year, 2));
		}
		if (format >= 4)
			cTime.append(getFormatTime(month, 2));
		if (format >= 6)
			cTime.append(getFormatTime(day, 2));
		if (format >= 8)
			cTime.append(getFormatTime(hour, 2));
		if (format >= 10)
			cTime.append(getFormatTime(minute, 2));
		if (format >= 12)
			cTime.append(getFormatTime(second, 2));
		if (format >= 15)
			cTime.append(getFormatTime(miltime, 3));
		return cTime.toString();
	}
	
	private static String getFormatTime(int time, int format)
	{
		StringBuffer numm = new StringBuffer(format);
		int length = String.valueOf(time).length();

		if (format < length)
			return null;

		for (int i = 0; i < format - length; i++)
		{
			numm.append("0");
		}
		numm.append(time);
		return numm.toString().trim();
	}

	/**
	 * @Description 当前年份
	 * 
	 * @param len
	 *            int 返回位数 2位 4位
	 * @return String 当前年份
	 */
	
	public static String getYear(int len)
	{
		Calendar time = Calendar.getInstance();
		int year = time.get(Calendar.YEAR);
		String djyear = Integer.toString(year);
		if (len == 2)
		{
			djyear = djyear.substring(2);
		}
		return djyear;
	}

	/**
	 * @Description 当前月份
	 * 
	 * @return String 当前月份（2位）
	 */
	
	public static String getMonth()
	{
		Calendar time = Calendar.getInstance();
		int month = time.get(Calendar.MONTH) + 1;
		String djmonth = "";
		if (month < 10)
		{
			djmonth = "0" + Integer.toString(month);
		}
		else
		{
			djmonth = Integer.toString(month);
		}
		return djmonth;
	}


	/**
	 * @Description 当前年份
	 * 
	 * @return String 当前年份（2位）
	 */
	
	public static String getYear()
	{
		Calendar time = Calendar.getInstance();
		int year = time.get(Calendar.YEAR) + 1;

		return String.valueOf(year);
	}
	
	/**
	 * @Description 当前天数
	 * 
	 * @return String 当前天数
	 */
	
	public static String getDay()
	{
		Calendar time = Calendar.getInstance();
		int day = time.get(Calendar.DAY_OF_MONTH);
		String djday = "";
		if (day < 10)
		{
			djday = "0" + Integer.toString(day);
		}
		else
		{
			djday = Integer.toString(day);
		}
		return djday;
	}
	
	/**
	 * @Description 当前小时
	 * 
	 * @return String 当前小时
	 */
	
	public static String getHour()
	{
		Calendar time = Calendar.getInstance();
		int hour = time.get(Calendar.HOUR_OF_DAY);
		String djhour = "";
		if (hour < 10)
		{
			djhour = "0" + Integer.toString(hour);
		}
		else
		{
			djhour = Integer.toString(hour);
		}
		return djhour;
	}
	
	/**
	 * @Description 当前分钟
	 * 
	 * @return String 当前分钟
	 */
	
	public static String getMin()
	{
		Calendar time = Calendar.getInstance();
		int min = time.get(Calendar.MINUTE);
		String djmin = "";
		if (min < 10)
		{
			djmin = "0" + Integer.toString(min);
		}
		else
		{
			djmin = Integer.toString(min);
		}
		return djmin;
	}
	
	/**
	 * @Description 格式化时间
	 * 
	 * @param time
	 * 		  String 时间    可为6位、8位、12位、15位
	 * 
	 * @return String 格式化后的时间 6位YY年MM月DD日、8位 YYYY年MM月DD日 、12位 YY年MM月DD日 、15位 YY年MM月DD日
	 */

	public static String formattime(String time)
	{
		int length = 0;
		if (time == null || time.length() < 6)
		{
			return "";
		}
		length = time.length();
		String renstr = "";
		switch (length)
		{
			case 6:
				renstr = time.substring(0, 2) + "-" + time.substring(2, 4)
						+ "-" + time.substring(4) + "";
				break;
			case 8:
				renstr = time.substring(0, 4) + "-" + time.substring(4, 6)
						+ "-" + time.substring(6, 8) + "";
				break;
			case 12:
				renstr = time.substring(0, 2) + "-" + time.substring(2, 4)
						+ "-" + time.substring(4, 6) + " "
						+ time.substring(6, 8) + ":" + time.substring(8, 10)
						+ ":" + time.substring(10, 12) + "";
				break;
			case 14:
				renstr = time.substring(0, 4) + "-" + time.substring(4, 6)
						+ "-" + time.substring(6, 8) + " "
						+ time.substring(8, 10) + ":" + time.substring(10, 12)
						+ ":" + time.substring(12, 14) + "";
				break;
			case 15:
				renstr = time.substring(0, 2) + "-" + time.substring(2, 4)
						+ "-" + time.substring(4, 6) + " "
						+ time.substring(6, 8) + ":" + time.substring(8, 10)
						+ ":" + time.substring(10, 12) + ":"
						+ time.substring(12);
				break;
			case 17:
				renstr = time.substring(0, 4) + "-" + time.substring(4, 6)
						+ "-" + time.substring(6, 8) + " "
						+ time.substring(8, 10) + ":" + time.substring(10, 12)
						+ ":" + time.substring(12, 14) + "";
				break;
			default:
				renstr = time.substring(0, 2) + "-" + time.substring(2, 4)
						+ "-" + time.substring(4) + "";
				break;
		}
		return renstr;
	}

	/**
	 * 根据系统时间得到n天以后的日期
	 * 
	 * @param int n 天数
	 * @return String 得到的时间 YYYYMMDD
	 */
	public static String getNDayLater(int n)
	{
		String time = null;
		String dayStr = null;
		String monthStr = null;
		String yearStr = null;
		java.util.Calendar cal = java.util.Calendar.getInstance();
		cal.add(java.util.Calendar.DAY_OF_MONTH, n);
		int day = cal.get(java.util.Calendar.DATE);
		int month = cal.get(java.util.Calendar.MONTH) + 1;
		int year = cal.get(java.util.Calendar.YEAR);
		if (day < 10)
			dayStr = "0" + Integer.toString(day);
		else
			dayStr = Integer.toString(day);
		if (month < 10)
			monthStr = "0" + Integer.toString(month);
		else
			monthStr = Integer.toString(month);
		yearStr = Integer.toString(year);
		time = yearStr + monthStr + dayStr;
		dayStr = null;
		monthStr = null;
		yearStr = null;
		System.out.println(time + "&&&&&&&&&&");
		return time;
	}

	/**
	 * 根据给定时间得到n天以后的日期
	 * 
	 * @param int n 天数
	 * @return String 得到的时间 YYYYMMDD
	 * @throws ParseException
	 */
	public static String getNDayLater(String date, int n)
	{
		String time = null;
		String dayStr = null;
		String monthStr = null;
		String yearStr = null;
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

		java.util.Calendar cal = new GregorianCalendar();
		try
		{
			cal.setTime(df.parse(formattime(date)));

			cal.add(java.util.Calendar.DAY_OF_MONTH, n);
			int day = cal.get(java.util.Calendar.DATE);
			int month = cal.get(java.util.Calendar.MONTH) + 1;
			int year = cal.get(java.util.Calendar.YEAR);
			if (day < 10)
				dayStr = "0" + Integer.toString(day);
			else
				dayStr = Integer.toString(day);
			if (month < 10)
				monthStr = "0" + Integer.toString(month);
			else
				monthStr = Integer.toString(month);
			yearStr = Integer.toString(year);
			time = yearStr + monthStr + dayStr;
			dayStr = null;
			monthStr = null;
			yearStr = null;
		}
		catch (ParseException e)
		{

			e.printStackTrace();
		}
		return time;
	}

	/**
	 * 返回给定时间内按周（周一至周日为一个单位）划分得到的时间数组
	 * 
	 * @param stime
	 *            String YYYYMMDD
	 * @param etime
	 *            String YYYYMMDD
	 * @return Vector
	 */
	public static Vector getWeekArray(String stime, String etime)
	{
		if (stime == null || stime.equals("") || etime == null
				|| etime.equals("") || stime.length() < 8 || etime.length() < 8
				|| stime.compareTo(etime) > 0)
		{
			return null;
		}

		Vector v = new Vector();
		String[] s = null;
		if (stime.equals(etime))
		{
			s = new String[2];
			s[0] = stime;
			s[1] = etime;
			v.add(s);
		}
		else
		{
			int year = 0;
			int month = 0;
			int date = 0;

			year = Integer.parseInt(stime.substring(0, 4));
			month = Integer.parseInt(stime.substring(4, 6));
			date = Integer.parseInt(stime.substring(6, 8));

			Calendar c = Calendar.getInstance();
			c.set(year, month - 1, date);

			int week = c.get(Calendar.DAY_OF_WEEK);
			int temp = 0;
			int i = 0;
			if (week != 2)
			{
				i++;
				s = new String[2];
				if (week == 1)
				{
					temp = 0;
				}
				else
				{
					temp = 8 - week;
				}
				s = new String[2];
				year = c.get(Calendar.YEAR);
				month = c.get(Calendar.MONTH) + 1;
				date = c.get(Calendar.DATE);
				s[0] = year + get00String(month) + get00String(date);

				c.add(Calendar.DAY_OF_MONTH, temp);

				year = c.get(Calendar.YEAR);
				month = c.get(Calendar.MONTH) + 1;
				date = c.get(Calendar.DATE);
				s[1] = year + get00String(month) + get00String(date);

				v.add(s);
				c.add(Calendar.DAY_OF_MONTH, 1);
				stime = year + get00String(month) + get00String(date);
			}

			while (!stime.equals(etime))
			{
				i++;
				s = new String[2];
				year = c.get(Calendar.YEAR);
				month = c.get(Calendar.MONTH) + 1;
				date = c.get(Calendar.DATE);
				s[0] = year + get00String(month) + get00String(date);

				c.add(Calendar.DAY_OF_MONTH, 6);
				year = c.get(Calendar.YEAR);
				month = c.get(Calendar.MONTH) + 1;
				date = c.get(Calendar.DATE);
				s[1] = year + get00String(month) + get00String(date);

				stime = year + get00String(month) + get00String(date);
				if (stime.compareTo(etime) > 0)
				{
					s[1] = etime;
					v.add(s);
					break;
				}

				v.add(s);

				c.add(Calendar.DAY_OF_MONTH, 1);
				stime = year + get00String(month) + get00String(date);
			}
		}
		return v;
	}

	/**
	 * 将日期时间从一种格式转换为另一种格式
	 * 
	 * @param srcTime
	 *            源串
	 * @param srcPattern
	 *            源串格式
	 * @param destPattern
	 *            目标串格式
	 * @return String 目标串
	 */
	public static String transDateTime(String srcTime, String srcPattern,
			String destPattern)
	{
		if (srcTime == null)
			return "";
		try
		{
			SimpleDateFormat fmt = new SimpleDateFormat();
			fmt.applyPattern(srcPattern);
			Date date = fmt.parse(srcTime);
			fmt.applyPattern(destPattern);
			return fmt.format(date);
		}
		catch (Exception exp)
		{
		}
		return srcTime;
	}

	/**
	 * 将14位日期格式转换成yyyy-MM-dd HH:mm:ss格式
	 * 
	 * @param srcDate
	 *            String
	 * @return String
	 */
	public static String transDate(String srcDate)
	{
		if (srcDate != null)
		{
			if (srcDate.trim().length() == 14)
				return transDateTime(srcDate.trim(), "yyyyMMddHHmmss",
						"yyyy-MM-dd HH:mm:ss");
			if (srcDate.trim().length() <= 8)
				return transDateTime(srcDate.trim(), "yyyyMMdd", "yyyy-MM-dd");
			return "";
		}
		else
			return "";
	}

	/**
	 * 将6位时间格式转换成HH:mm:ss或者HH:mm格式
	 * 
	 * @param srcTime
	 * @param len
	 *            (2:HH:mm,3HH:mm:ss)
	 * @return
	 */
	public static String transTime(String srcTime, int len)
	{
		String temp = "";
		if (len == 2)
		{
			temp = srcTime.substring(0, 2) + ":" + srcTime.substring(3, 5);
		}
		else if (len == 3)
		{
			temp = srcTime.substring(0, 2) + ":" + srcTime.substring(3, 5)
					+ ":" + srcTime.substring(6, 8);
		}
		else
		{
			temp = "0";
		}

		return temp;
	}

	/**
	 * 将14位日期格式转换成MM-dd HH:mm格式
	 * 
	 * @param srcDate
	 *            String
	 * @return String
	 */
	
	public static String transDateSimple(String srcDate)
	{
		if (srcDate != null)
		{
			if (srcDate.trim().length() == 14)
				return transDate(srcDate).substring(5, 16);
			return "";
		}
		else
			return "";
	}

	public static String get00String(int iT)
	{
		if (iT >= 10)
		{
			return String.valueOf(iT);
		}
		else
			return "0" + String.valueOf(iT);

	}



	/**
	 * 取得本地系统的时间，时间格式由参数决定
	 * 
	 * @param format
	 *            时间格式由常量决定
	 * @return String 具有format格式的字符串
	 */
	public String getTimeIng(int format)
	{
		StringBuffer cTime = new StringBuffer(15);
		Calendar time = Calendar.getInstance();
		int miltime = time.get(Calendar.MILLISECOND);
		int second = time.get(Calendar.SECOND);
		int minute = time.get(Calendar.MINUTE);
		int hour = time.get(Calendar.HOUR_OF_DAY);
		int day = time.get(Calendar.DAY_OF_MONTH);
		int month = time.get(Calendar.MONTH) + 1;
		int year = time.get(Calendar.YEAR);
		time = null;
		if (format == YYYYMMDD)
		{
			return cTime.append(year).append(getFormatTime(month, 2)).append(
					getFormatTime(day, 2)).toString().trim();
		}
		if (format != 14)
		{
			if (year >= 2000)
				year = year - 2000;
			else
				year = year - 1900;
		}
		if (format >= 2)
		{
			if (format == 14)
				cTime.append(year);
			else
				cTime.append(getFormatTime(year, 2));
		}
		if (format >= 4)
			cTime.append(getFormatTime(month, 2));
		if (format >= 6)
			cTime.append(getFormatTime(day, 2));
		if (format >= 8)
			cTime.append(getFormatTime(hour, 2));
		if (format >= 10)
			cTime.append(getFormatTime(minute, 2));
		if (format >= 12)
			cTime.append(getFormatTime(second, 2));
		if (format >= 15)
			cTime.append(getFormatTime(miltime, 3));
		return cTime.toString().trim();
	}
	/**
	 * 将4位日期控件中的日期格式yyyy转换成字符串格式
	 * 
	 * @param date
	 *            Date
	 * @return String
	 */
	public static String dateToStringYear(Date date)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		String str = sdf.format(date);
		return str;
	}
	/**
	 * 将6位日期格式yyyy-MM转换成字符串格式
	 * 
	 * @param date
	 *            Date
	 * @return String
	 */
	public static String date6ToString(Date date)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		String str = sdf.format(date);
		return str;
	}
	/**
	 * 将10位日期格式yyyy-MM-dd转换成字符串格式
	 * 
	 * @param date
	 *            Date
	 * @return String
	 */
	public static String dateToString(Date date)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));//设置系统时区
		String str = sdf.format(date);
		return str;
	}

	/**
	 * 将10位日期格式yyyy-MM-dd HH:mm:ss转换成字符串格式
	 * 
	 * @param date
	 *            Date
	 * @return String
	 */
	public static String dateToStrings(Date date)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));//设置系统时区
		String str = sdf.format(date);
		return str;
	}
	public static String dateToYyyyMMddHHmmssSSS(Date date)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));//设置系统时区
		String str = sdf.format(date);
		return str;
	}

	/**
	 * 计算时间差 和设置时间比较 进行 报警
	 * 
	 * @param oldtime
	 *            年月日时间字符串 如:20100429
	 * @param settime
	 *            小时 如：5小时
	 * @return true : 超过设定时间 报警 false : 正常
	 * */
	public static boolean sign(String oldtime, String settime)
	{
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		boolean flag = false;
		try
		{
			Date old = df.parse(CTime.formattime(oldtime));
			Date now = new Date();
			long dtime = now.getTime() - old.getTime();
			int dt = (int) dtime;
			int stime = Integer.parseInt(settime) * 60 * 60 * 1000;
			int n = dt - stime;
			if (n > 0)
			{
				flag = true;
			}
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		return flag;
	}

	/**
	 * 小时加减
	 * 
	 * @param time
	 *            具体时间
	 * @param min
	 *            加减分钟数
	 * @return 加减后的时间
	 */
	public static String addDataTime(String time, String min)
	{
		String strNext = "";
		try
		{

			String date = time.substring(0, 4) + "-" + time.substring(4, 6)
					+ "-" + time.substring(6, 8) + " " + time.substring(8, 10)
					+ ":" + time.substring(10, 12) + ":"
					+ time.substring(12, 14);
			java.text.SimpleDateFormat d = new java.text.SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			java.util.Date dt;

			dt = d.parse(date);

			long now = dt.getTime();
			long next = now + Long.parseLong(min) * 60 * 1000;

			Date dtNext = new Date(next);

			SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
			strNext = df.format(dtNext);
		}
		catch (ParseException e)
		{

			e.printStackTrace();
		}
		return strNext;
	}



	/**
	 * 时间差值（分钟）
	 * 
	 * @param starttime
	 *            开始时间（yyyyMMddhhmmss）
	 * @param endtime
	 *            结束时间（yyyyMMddhhmmss）
	 * @return 差值分钟
	 */
	public static String diffValue(String starttime, String endtime)
	{
		String strNext = "";
		try
		{
			Calendar c = Calendar.getInstance();
			int year = Integer.parseInt(starttime.substring(0, 4));
			int month = Integer.parseInt(starttime.substring(4, 6));
			int date = Integer.parseInt(starttime.substring(6, 8));
			int hourOfDay = Integer.parseInt(starttime.substring(8, 10));
			int minute = Integer.parseInt(starttime.substring(10, 12));
			int second = Integer.parseInt(starttime.substring(12, 14));
			c.set(year, month, date, hourOfDay, minute, second);

			int year1 = Integer.parseInt(endtime.substring(0, 4));
			int month1 = Integer.parseInt(endtime.substring(4, 6));
			int date1 = Integer.parseInt(endtime.substring(6, 8));
			int hourOfDay1 = Integer.parseInt(endtime.substring(8, 10));
			int minute1 = Integer.parseInt(endtime.substring(10, 12));
			int second1 = Integer.parseInt(endtime.substring(12, 14));

			Calendar c1 = Calendar.getInstance();
			c1.set(year1, month1, date1, hourOfDay1, minute1, second1);

			long l = c1.getTimeInMillis() - c.getTimeInMillis();
			strNext = String.valueOf(l / (60 * 1000));
		}
		catch (Exception e)
		{

			e.printStackTrace();
		}
		return strNext;
	}

	/**
	 * 计算时间差 和当前时间比较
	 * 
	 * @param oldtime
	 *            年月日时间字符串 如:20100429
	 * @return 时间差 long
	 * */
	public static long diffValue(String oldtime)
	{
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long dtime = 0;
		try
		{
			Date old = df.parse(CTime.formattime(oldtime));
			Date now = new Date();
			dtime = (now.getTime() - old.getTime()) / 60 / 60 / 1000;
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}

		return dtime;
	}

	/**
	 * 两天之间隔几天
	 * 
	 * @param starttime
	 *            开始日期（yyyyMMdd）
	 * @param endtime
	 *            结束日期（yyyyMMdd）
	 * @return 天数
	 * @throws ParseException
	 */

	public static int dateDiff(String starttime, String endtime)

	{
		int strNext = 0;
		try
		{
			Calendar c = Calendar.getInstance();
			int year = Integer.parseInt(starttime.substring(0, 4));
			int month = Integer.parseInt(starttime.substring(4, 6));
			int date = Integer.parseInt(starttime.substring(6, 8));

			c.set(year, month, date);

			int year1 = Integer.parseInt(endtime.substring(0, 4));
			int month1 = Integer.parseInt(endtime.substring(4, 6));
			int date1 = Integer.parseInt(endtime.substring(6, 8));

			Calendar c1 = Calendar.getInstance();
			c1.set(year1, month1, date1);
			long l = c1.getTimeInMillis() - c.getTimeInMillis();
			strNext = (int) (l / (3600 * 24 * 1000));
		}
		catch (Exception e)
		{

			e.printStackTrace();
		}
		return strNext;

	}

	/**
	 * @Description Date 转换 成Timestamp
	 * @param date
	 * 			Date 时间
	 * @return 格式化后时间 Timestamp
	 * */
	public static Timestamp dateToTimestamp(Date date)
	{
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time=df.format(date);
		Timestamp tamp=Timestamp.valueOf(time);
		return tamp;
	}
	/***
	 @Description Date 转换 成String
	 * @param date
	 * 			Date 时间(yyyy-MM-dd HH:mm)
	 * @return 格式化后时间 String
	 */
	public static String dateToTimeSF(Date date){
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String time=df.format(date);
		
		return time;
	}
	/***
	 @Description Date 转换 成String
	 * @param date
	 * 			Date 时间(yyyy-MM-dd)
	 * @return 格式化后时间 String
	 */
	public static String dateToTimeYMD(){
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
		df.setTimeZone(TimeZone.getTimeZone("GMT+8"));//设置系统时区
		String time=df.format(new Date());
		return time;
	}
	/***
	 @Description Date 转换 成String
	 * @param date
	 * 			Date 时间(yyyy-MM-dd)
	 * @return 格式化后时间 String
	 */
	public static String dateToTimeYMDHMS(Date date){
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		df.setTimeZone(TimeZone.getTimeZone("GMT+8"));//设置系统时区
		String time=df.format(date);
		return time;
	}
	/**
	 * Description 获取当前系统时间  时间格式yyyy-MM-dd HH:mm:ss
	 * @return 格式化后时间 String
	 * */
	public static String dateToTimeYMDHMS(){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));//设置系统时区
		Date curDate = new Date();//获取系统时间
		return sdf.format(curDate);
	}
	/**
	 * 计算某一日期几天之后的日期
	 * @param Date now 参照日期
	 * @param long days 天数
	 * @return 返回计算日期 yyyy-MM-dd 字符串
	 * */
	public static String newDateYMD(Date now,long days){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");	
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));//设置系统时区
        Calendar cal = Calendar.getInstance();		
        cal.setTime(now);		
        long timeone = cal.getTimeInMillis();		
        long result = timeone + days * 24 * 60 * 60 * 1000;		
        Date getDate = new Date(result);
        return sdf.format(getDate);
	}
	/**
	 * 计算某一时间几天之后的时间
	 * @param Date now 参照日期
	 * @param long days 天数
	 * @return 返回计算时间
	 * */
	public static Date newDateTimes(Date now,long days){
        Calendar cal = Calendar.getInstance();		
        cal.setTime(now);		
        long timeone = cal.getTimeInMillis();		
        long result = timeone + days * 24 * 60 * 60 * 1000;		
        Date getDate = new Date(result);
        return getDate;
	}
	/**
	 * 获取某年某个月的天数
	 * @param String dyear 某年
	 * @param String dmouth 某月
	 * @return 返回某月天数
	 * */
	public static long calDayByYearAndMonth(String dyear,String dmouth){
		SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy/MM");
	    Calendar rightNow = Calendar.getInstance();
	    try{
	    	rightNow.setTime(simpleDate.parse(dyear+"/"+dmouth));
	    }catch(ParseException e){
	    	e.printStackTrace();
	    }
	    return rightNow.getActualMaximum(Calendar.DAY_OF_MONTH);//根据年月 获取月份天数
	}
	/**
	 * yyyy-MM-dd HH:mm:ss 格式时间转为yyyy/MM/dd HH:mm:ss格式时间
	 * */
	public static String tmpDateTimes(Date date) throws Exception{
		String times=date+"";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date dates=sdf.parse(times);
		String str=sdf2.format(dates);
		return str;
	}
	
	public static long getTimes(String datetime){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long times=0;;
		try {
			Date date=sdf.parse(datetime);
			times=date.getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return times;
	}
	
//	public static void main(String[] arg){
//
//	}
}

